/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package controller

import (
	"encoding/json"
	"fmt"
	"github.com/mjiulee/lego"
	"github.com/valyala/fasthttp"
	. "saas_sys_base/modules/oss/service"
)

type AliOssCtrl struct {
	service AliOssService
}

func init() {
	vc := new(AliOssCtrl)
	_ctrlList = append(_ctrlList, vc)
}

func (vc *AliOssCtrl) CtrlInfo() string {
	return "AliOssCtrl"
}
//阿里云API应用场景参考
//https://help.aliyun.com/document_detail/31927.html?spm=a2c4g.11186623.2.14.52236e28NfDGMb#concept-qp2-g4y-5db
func (vc *AliOssCtrl) RoutInit() {
	//lego.APIGET("/api/mod/oss/alioss/get_policy_token", vc.GetPolicyToken, false)  //TODO 权限校验开启
	lego.APIPOST("/api/mod/oss/alioss/get_policy_token", vc.GetPolicyToken, false) //TODO 权限校验开启
	lego.APIPOSTWITHOUTSIGN("/api/mod/oss/alioss/oss_callback", vc.OssCallback)
	//lego.APIPOST("/api/mod/oss/alioss/get_policy_token", vc.OssCallback, false)
}

func (t *AliOssCtrl) GetPolicyToken(ctx *fasthttp.RequestCtx) {
	t.service.GetPolicyTokenHandler(ctx)
}

type CallbackBodyDefaultStru struct {
	Filename string `json:"filename"`
	Size     string `json:"size"`
	MimeType string `json:"mimeType"`
	Height   string `json:"height"`
	Width    string `json:"width"`
}

func (t *AliOssCtrl) OssCallback(ctx *fasthttp.RequestCtx) {
	filename := string(ctx.Request.PostArgs().Peek("filename"))
	contentType := string(ctx.Request.Header.ContentType())
	fmt.Println("OssCallback filename:", filename)
	fmt.Println("OssCallback contentType:", contentType)

	var request CallbackBodyDefaultStru
	requestByte := ctx.PostBody()
	err := json.Unmarshal(requestByte, &request)
	if err != nil {

	}

	fmt.Println("OssCallback Unmarshal requestByte:", string(requestByte))
	fmt.Println("OssCallback Unmarshal filename:", request.Filename)

	t.service.OssCallbackhandler(ctx)
}
